var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
// var session = require('express-session');
// 引入jwt
// var { expressjwt: jwt } = require('express-jwt');
// var {secretKey,unless,algorithms} = require('./token');
var swaggerInstall = require('./utils/swagger')
// 引入路由
var storeRouter = require('./routes/store');
var breadTeaRouter = require('./routes/bread');
var coffeeRouter = require('./routes/coffee');
var dessertRouter = require('./routes/dessert');
var drinksRouter = require('./routes/drinks');
var feedingRouter = require('./routes/feeding');
var milkyTeaRouter = require('./routes/milkyTea');
var snacksRouter = require('./routes/snacks');
var adminRouter = require('./routes/admin');
var userRouter = require('./routes/user');
var orderRouter = require('./routes/order');
var rollRouter = require('./routes/roll');
var snapshotRouter = require('./routes/snapshot');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();
swaggerInstall(app);
require('./utils/mongooseUtil.js');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// app.use(session({
//   secret:"lovo",
//   resave:false, // 强制保存session，默认为true，即使它没有变化，建议false
//   saveUninitialized:true ,// 强制将未初始化的session存储（当新建一个sesison，但未设定属性和值时处于未初始化状态）
 
// }));
// 注册jwt
// app.use(jwt({secret:secretKey,algorithms}).unless(unless));
// 注册路由
app.use('/', indexRouter);
app.use('/store', storeRouter);
app.use('/bread', breadTeaRouter);
app.use('/coffee', coffeeRouter);
app.use('/dessert', dessertRouter);
app.use('/drinks', drinksRouter);
app.use('/feeding', feedingRouter);
app.use('/milkytea', milkyTeaRouter);
app.use('/snacks', snacksRouter);
app.use('/admin', adminRouter);
app.use('/user', userRouter);
app.use('/order', orderRouter);
app.use('/roll', rollRouter);
app.use('/snapshot', snapshotRouter);
app.use('/users', usersRouter);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  // if(err.status != 401){
  //   console.log(err);
  // }
  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
